import axios from "axios";
import { showFailToast } from "vant";

const request = axios.create({
  baseURL: "http://47.94.6.186:3005",
  timeout: 5000,
});

request.interceptors.request.use(
  (config) => {
    let authorization = localStorage.getItem("authorization");
    if (authorization) {
      config.headers.authorization = "Bearer " + authorization;
    }
    return config;
  },
  (err) => {
    Promise.reject(err);
  }
);

request.interceptors.response.use(
  (response) => {
    let { data } = response;
    return data;
  },
  (err) => {
    const { status, response } = err;
    let message = response.data.message;
    if (status !== 200) showFailToast(message);
    Promise.reject(err);
  }
);

export default request;
